﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.IDCard
{
	/// <summary>
	/// 基于SAM协议的Tcp实现的网络类，支持读取身份证的数据信息，通过透传的形式实现，除了初始化和串口类不一致，调用方法是几乎一模一样的，详细参见API文档<br />
	/// The network class implemented by Tcp based on the SAM protocol supports reading ID card data information and is implemented in the form of transparent transmission. 
	/// Except for the inconsistency between the initialization and the serial port class, the calling method is almost the same. 
	/// See the API documentation for details
	/// </summary>
	/// <example>
	/// 在使用之前需要实例化当前的对象，然后根据实际的情况填写好串口的信息，否则连接不上去。
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SAMSerialSample.cs" region="Sample3" title="实例化操作" />
	/// 在实际的读取，我们一般放在后台进行循环扫描的操作，参见下面的代码
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SAMSerialSample.cs" region="Sample4" title="基本的读取操作" />
	/// 当然也支持全异步的操作了，就是方法的名称改改
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SAMSerialSample.cs" region="Sample5" title="实例化操作" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\SAMSerialSample.cs" region="Sample6" title="基本的读取操作" />
	/// </example>
	// Token: 0x0200009C RID: 156
	public class SAMTcpNet : NetworkDoubleBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06000BB8 RID: 3000 RVA: 0x00024933 File Offset: 0x00022B33
		public SAMTcpNet()
		{
			base.ByteTransform = new RegularByteTransform();
		}

		/// <summary>
		/// 通过指定的ip地址以及端口来实例化对象<br />
		/// Instantiate the object with the specified IP address and port
		/// </summary>
		/// <param name="ipAddress">ip地址</param>
		/// <param name="port">端口号</param>
		// Token: 0x06000BB9 RID: 3001 RVA: 0x00024949 File Offset: 0x00022B49
		public SAMTcpNet(string ipAddress, int port)
		{
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x06000BBA RID: 3002 RVA: 0x00045460 File Offset: 0x00043660
		protected override INetMessage GetNewNetMessage()
		{
			return new SAMMessage();
		}

		/// <summary>
		/// 读取身份证设备的安全模块号<br />
		/// Read the security module number of the ID device
		/// </summary>
		/// <returns>结果数据内容</returns>
		// Token: 0x06000BBB RID: 3003 RVA: 0x00045468 File Offset: 0x00043668
		[HslMqttApi]
		public OperateResult<string> ReadSafeModuleNumber()
		{
			byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(18, byte.MaxValue, null));
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					result = SAMSerial.ExtractSafeModuleNumber(operateResult.Content);
				}
			}
			return result;
		}

		/// <summary>
		/// 检测安全模块状态<br />
		/// Detecting Security Module Status
		/// </summary>
		/// <returns>返回是否检测成功</returns>
		// Token: 0x06000BBC RID: 3004 RVA: 0x000454E0 File Offset: 0x000436E0
		[HslMqttApi]
		public OperateResult CheckSafeModuleStatus()
		{
			byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(18, byte.MaxValue, null));
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult.Content[9] != 144;
					if (flag3)
					{
						result = new OperateResult(SAMSerial.GetErrorDescription((int)operateResult.Content[9]));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 寻找卡片，并返回是否成功<br />
		/// Find cards and return success
		/// </summary>
		/// <returns>是否寻找成功</returns>
		// Token: 0x06000BBD RID: 3005 RVA: 0x00045580 File Offset: 0x00043780
		[HslMqttApi]
		public OperateResult SearchCard()
		{
			byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(32, 1, null));
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult.Content[9] != 159;
					if (flag3)
					{
						result = new OperateResult(SAMSerial.GetErrorDescription((int)operateResult.Content[9]));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 选择卡片，并返回是否成功<br />
		/// Select card and return success
		/// </summary>
		/// <returns>是否寻找成功</returns>
		// Token: 0x06000BBE RID: 3006 RVA: 0x0004561C File Offset: 0x0004381C
		[HslMqttApi]
		public OperateResult SelectCard()
		{
			byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(32, 2, null));
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult.Content[9] != 144;
					if (flag3)
					{
						result = new OperateResult(SAMSerial.GetErrorDescription((int)operateResult.Content[9]));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 读取卡片，如果成功的话，就返回身份证的所有的信息<br />
		/// Read the card, if successful, return all the information of the ID cards
		/// </summary>
		/// <returns>是否寻找成功</returns>
		// Token: 0x06000BBF RID: 3007 RVA: 0x000456B8 File Offset: 0x000438B8
		[HslMqttApi]
		public OperateResult<IdentityCard> ReadCard()
		{
			byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(48, 1, null));
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<IdentityCard> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<IdentityCard>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<IdentityCard>(operateResult2);
				}
				else
				{
					result = SAMSerial.ExtractIdentityCard(operateResult.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.IDCard.SAMTcpNet.ReadSafeModuleNumber" />
		// Token: 0x06000BC0 RID: 3008 RVA: 0x0004572C File Offset: 0x0004392C
		[DebuggerStepThrough]
		public Task<OperateResult<string>> ReadSafeModuleNumberAsync()
		{
			SAMTcpNet.<ReadSafeModuleNumberAsync>d__8 <ReadSafeModuleNumberAsync>d__ = new SAMTcpNet.<ReadSafeModuleNumberAsync>d__8();
			<ReadSafeModuleNumberAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadSafeModuleNumberAsync>d__.<>4__this = this;
			<ReadSafeModuleNumberAsync>d__.<>1__state = -1;
			<ReadSafeModuleNumberAsync>d__.<>t__builder.Start<SAMTcpNet.<ReadSafeModuleNumberAsync>d__8>(ref <ReadSafeModuleNumberAsync>d__);
			return <ReadSafeModuleNumberAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.IDCard.SAMTcpNet.CheckSafeModuleStatus" />
		// Token: 0x06000BC1 RID: 3009 RVA: 0x00045770 File Offset: 0x00043970
		[DebuggerStepThrough]
		public Task<OperateResult> CheckSafeModuleStatusAsync()
		{
			SAMTcpNet.<CheckSafeModuleStatusAsync>d__9 <CheckSafeModuleStatusAsync>d__ = new SAMTcpNet.<CheckSafeModuleStatusAsync>d__9();
			<CheckSafeModuleStatusAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<CheckSafeModuleStatusAsync>d__.<>4__this = this;
			<CheckSafeModuleStatusAsync>d__.<>1__state = -1;
			<CheckSafeModuleStatusAsync>d__.<>t__builder.Start<SAMTcpNet.<CheckSafeModuleStatusAsync>d__9>(ref <CheckSafeModuleStatusAsync>d__);
			return <CheckSafeModuleStatusAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.IDCard.SAMTcpNet.SearchCard" />
		// Token: 0x06000BC2 RID: 3010 RVA: 0x000457B4 File Offset: 0x000439B4
		[DebuggerStepThrough]
		public Task<OperateResult> SearchCardAsync()
		{
			SAMTcpNet.<SearchCardAsync>d__10 <SearchCardAsync>d__ = new SAMTcpNet.<SearchCardAsync>d__10();
			<SearchCardAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SearchCardAsync>d__.<>4__this = this;
			<SearchCardAsync>d__.<>1__state = -1;
			<SearchCardAsync>d__.<>t__builder.Start<SAMTcpNet.<SearchCardAsync>d__10>(ref <SearchCardAsync>d__);
			return <SearchCardAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.IDCard.SAMTcpNet.SelectCard" />
		// Token: 0x06000BC3 RID: 3011 RVA: 0x000457F8 File Offset: 0x000439F8
		[DebuggerStepThrough]
		public Task<OperateResult> SelectCardAsync()
		{
			SAMTcpNet.<SelectCardAsync>d__11 <SelectCardAsync>d__ = new SAMTcpNet.<SelectCardAsync>d__11();
			<SelectCardAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SelectCardAsync>d__.<>4__this = this;
			<SelectCardAsync>d__.<>1__state = -1;
			<SelectCardAsync>d__.<>t__builder.Start<SAMTcpNet.<SelectCardAsync>d__11>(ref <SelectCardAsync>d__);
			return <SelectCardAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.IDCard.SAMTcpNet.ReadCard" />
		// Token: 0x06000BC4 RID: 3012 RVA: 0x0004583C File Offset: 0x00043A3C
		[DebuggerStepThrough]
		public Task<OperateResult<IdentityCard>> ReadCardAsync()
		{
			SAMTcpNet.<ReadCardAsync>d__12 <ReadCardAsync>d__ = new SAMTcpNet.<ReadCardAsync>d__12();
			<ReadCardAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<IdentityCard>>.Create();
			<ReadCardAsync>d__.<>4__this = this;
			<ReadCardAsync>d__.<>1__state = -1;
			<ReadCardAsync>d__.<>t__builder.Start<SAMTcpNet.<ReadCardAsync>d__12>(ref <ReadCardAsync>d__);
			return <ReadCardAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000BC5 RID: 3013 RVA: 0x00045880 File Offset: 0x00043A80
		public override string ToString()
		{
			return string.Format("SAMTcpNet[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
